package com.coderman.business.service.imp;

import com.coderman.business.converter.ConsultantConverter;
import com.coderman.business.mapper.ConsultantMapper;
import com.coderman.business.mapper.PayMentMapper;
import com.coderman.business.mapper.StudentMapper;
import com.coderman.business.service.IConsultantService;
import com.coderman.business.service.base.AbstractServiceImpl;
import com.coderman.common.error.SystemCodeEnum;
import com.coderman.common.error.SystemException;
import com.coderman.common.model.consult.Consultant;
import com.coderman.common.model.payMent.PayMent;
import com.coderman.common.model.student.Student;
import com.coderman.common.response.ActiveUser;
import com.coderman.common.vo.business.ConsultVO;
import com.coderman.common.vo.system.PageVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 咨询信息管理业务层的实现
 * @author  文晓平
 * @Date 2023-1-9
 * @version 1.0
 */
@Service
public class ConsultantServiceImpl extends AbstractServiceImpl<ConsultantMapper, Consultant, Integer> implements IConsultantService {

    @Autowired
    private ConsultantMapper consultantMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private PayMentMapper payMentMapper;
    @Override
    public PageVO<Consultant> findList(Integer pageNum, Integer pageSize, ConsultVO consultVO) {
        //已经拥有的
        PageHelper.startPage(pageNum,pageSize);
        Example o = new Example(Consultant.class);
        String consultantName = consultVO.getConsultantName();
        Integer isSignup = consultVO.getIsSignup();
        String phone = consultVO.getPhone();
        String careerPlanner = consultVO.getCareerPlanner();
        Example.Criteria criteria = o.createCriteria();
        if(!StringUtils.isEmpty(consultantName)){
            criteria.andLike("consultantName","%"+consultantName+"%");
        }
        if(isSignup!=null){
            criteria.andEqualTo("isSignup",isSignup);
        }
        if(!StringUtils.isEmpty(phone)){
            criteria.andEqualTo("phone",phone);
        }
        if(!StringUtils.isEmpty(careerPlanner)){
            criteria.andEqualTo("careerPlanner",careerPlanner);
        }

        List<Consultant> classList= consultantMapper.selectByExample(o);
        PageInfo<Consultant> info=new PageInfo<>(classList);
        return new PageVO<>(info.getTotal(),classList);
    }

    /**
     * 添加咨询者信息
     * 同时添加到学生表
     *
     * @param consultVO
     */
    @Override
    public void add(ConsultVO consultVO) {
        Consultant consultant = new Consultant();
        BeanUtils.copyProperties(consultVO,consultant);
        Student student = new Student();
        student.setStudentName(consultant.getConsultantName());
        student.setStudentGender(consultant.getSex());
        student.setStudentPhone(consultant.getPhone());
        student.setStudnetAddress(consultant.getAddress());
        student.setWechatAaccout(consultant.getWechatNumber());
        student.setPlanner(consultant.getCareerPlanner());
        student.setStudentStatus(0);
        int result = consultantMapper.insert(consultant);
        if(result>0) {
            studentMapper.addStudent(student);
        }
    }

    /**
     * 编辑咨询信息
     *
     * @param id
     * @return
     */
    @Override
    public ConsultVO edit(Integer id) throws SystemException {
        Consultant consultant = consultantMapper.selectByPrimaryKey(id);
        if(consultant==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的咨询信息不存在");
        }
        ConsultVO consultVO = ConsultantConverter.converterToClassVO(consultant);
        return consultVO;
    }

    /**
     * 更新咨询信息
     *
     * @param id
     * @param consultVO
     */
    @Override
    public void update(Integer id, ConsultVO consultVO) throws SystemException {
        Consultant dbConsultant = consultantMapper.selectByPrimaryKey(id);
        if(dbConsultant==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"编辑的咨询信息不存在");
        }
        Consultant consultant = new Consultant();
        BeanUtils.copyProperties(consultVO,consultant);
        consultant.setConsultantId(id);
        consultantMapper.updateByPrimaryKeySelective(consultant);
    }

    /**
     * 删除咨询信息
     *
     * @param id
     */
    @Override
    public void delete(Long id) throws SystemException {
        Consultant consultant = consultantMapper.selectByPrimaryKey(id);
        if(consultant==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要删除的咨询信息不存在");
        }
        consultantMapper.deleteByPrimaryKey(id);
    }

    /**
     * 所有咨询信息
     *
     * @return
     */
    @Override
    public List<ConsultVO> findAllVO() {
        List<Consultant> consultants = consultantMapper.selectAll();
        //转vo
        List<ConsultVO> consultVOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(consultants)) {
            for (Consultant consultant : consultants) {
                ConsultVO u = new ConsultVO();
                BeanUtils.copyProperties(consultant, u);
                Example o = new Example(Consultant.class);
                o.createCriteria().andEqualTo("consultantId",consultant.getConsultantId())
                        .andNotEqualTo("type",0);
                consultVOS.add(u);
            }
        }
        return consultVOS;
    }

    /**
     * 全部咨询信息
     *
     * @return
     */
    @Override
    public List<Consultant> findAll() {
        return null;
    }

    /**
     * 更新状态
     *
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Integer id, Integer status) throws SystemException {
        Consultant dbConsultant = consultantMapper.selectByPrimaryKey(id);
        if(dbConsultant==null){
            throw new SystemException(SystemCodeEnum.PARAMETER_ERROR,"要更新状态的咨询者不存在");
        }
        ActiveUser activeUser= (ActiveUser) SecurityUtils.getSubject().getPrincipal();

        Consultant consultant = new Consultant();
        consultant.setConsultantId(id);
        consultant.setIsSignup(status);
        consultantMapper.updateByPrimaryKeySelective(consultant);
            //先去学生表查询信息，如果不为空，则插入一条数据到缴费表
        if(status==1){  //更新为已报名
            Student stu = new Student();
            stu.setStudentName(dbConsultant.getConsultantName());
            stu.setStudentPhone(dbConsultant.getPhone());
            Student student = studentMapper.selectOne(stu);
            if(student!=null){
                //存入到缴费表  先判断是否有计录，没有记录才添加
                PayMent dbPa = new PayMent();
                dbPa.setStudentid(student.getStudentId());
                PayMent payM = payMentMapper.selectOne(dbPa);
                if(payM==null) {
                    payM = new PayMent();
                    payM.setStudentid(student.getStudentId());
                    payMentMapper.insert(payM);
                }
            }
        }
    }
}
